// pages/test/test.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    examQuestionList: [],
    right: 0,
    error: 0,
    curretQuestionAnswer: "",
    answerOk: false,
    createTime: new Date()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var app = getApp();
    var testUrl = app.testUrl;
    var that = this;
    wx.request({
      url: testUrl + '/examQuestion/search.do?page=1&rows=20',
      method: "post",
      data: { "lessonId": 1 },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var data = res.data.rows
        //将问题选项从json字符串转换为json对象
        for (var i = 0; i < data.length; i++) {
          data[i].answer = JSON.parse(data[i].answer)
          data[i].complete = false;//默认为未回答
        }
        that.setData({
          //试题列表
          examQuestionList: data,
          currentQuestionIndex: 0
        });
        console.log(that.data)
      }
    });




  },
  nextQuestion: function (e) {

    if (this.data.currentQuestionIndex < this.data.examQuestionList.length) {
      this.verifyAnswer();
      if (this.data.currentQuestionIndex < this.data.examQuestionList.length - 1) {
        this.setData({
          currentQuestionIndex: this.data.currentQuestionIndex + 1,
          curretQuestionAnswer: ""
        });
      }
    }
    console.log(this.data)
  },
  preQuestion: function () {
    if (this.data.currentQuestionIndex > 0){
      this.setData({
        currentQuestionIndex: this.data.currentQuestionIndex - 1,
        curretQuestionAnswer: ""
      });
    }
  },
  //用户点击选项后触发，记录用户当前选择的答案
  setCurretQuestionAnswer: function (e) {
    if (this.data.examQuestionList[this.data.currentQuestionIndex].complete) return;
    this.setData({
      curretQuestionAnswer: e.currentTarget.dataset.answeroption
    });
  },
  //验证用户选择的答案和正确答案是否一致
  verifyAnswer: function () {
    //判断用户是否已经做完了所有题目
    if (this.data.right + this.data.error == this.data.examQuestionList.length) { return }
    //判断用户答案是否正确
    var flag = this.data.curretQuestionAnswer == this.data.examQuestionList[this.data.currentQuestionIndex].rightAnswer;
     this.data.examQuestionList[this.data.currentQuestionIndex].userAnswer = this.data.curretQuestionAnswer;
      this.data.examQuestionList[this.data.currentQuestionIndex].complete = true;
    if (flag) {
    
      //记录正确答案数
      this.setData({
        right: this.data.right + 1,
        examQuestionList: this.data.examQuestionList
      });
    } else {
      //记录错误答案数
      this.setData({
        error: this.data.error + 1,
        examQuestionList: this.data.examQuestionList
      });
    }

  },
  // 交卷
  sumitPaper: function (e) {
    var app = getApp();
    var testUrl = app.testUrl;
    var that = this;
    var score = this.data.right * 5;
    console.log(score);
    wx.request({
      method: "post",
      url: testUrl + '/lessonExam/add.do',
      data: {
        "lessonId": 1,
        "score": score,
        "userId": "1",
        "submitTime": new Date(),
        "createtime": this.data.createTime
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {

        console.log(res)
        if(res.data.success==true){
          wx.navigateTo({
            url: '/pages/msg/testmsg_success?score=' + score
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})